<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>概念_业务状态码</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
    <style>
        html,
        body {
            background-color: #f8f8f8;
        }

        .login-box {
            width: 400px;
            position: fixed;
            top: 20%;
            left: 50%;
            transform: translateX(-50%);
            border: 1px solid #efefef;
            padding: 20px;
            border-radius: 4px;
            box-shadow: 1px 1px 5px #cfcfcf;
            background-color: #fff;
            transition: box-shadow 0.3s ease;
        }

        .login-box:hover {
            transition: box-shadow 0.3s ease;
            box-shadow: 1px 1px 20px #cfcfcf;
        }
    </style>
</head>

<body>
    <div class="login-box">
        <div class="form-group">
            <label for="username">Account</label>
            <!-- 账号 -->
            <input type="text" class="form-control" id="username" autocomplete="off">
            <small id="emailHelp" class="form-text text-muted">The available account is <strong>admin</strong></small>
        </div>
        <div class="form-group">
            <!-- 密码 -->
            <label for="password">Password</label>
            <input type="password" class="form-control" id="password">
            <small id="emailHelp" class="form-text text-muted">The available password is <strong>123456</strong></small>
        </div>
        <button type="submit" class="btn btn-primary" id="btnLogin">Submit</button>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/axios@0.27.2/dist/axios.min.js"></script>
    <script>
        // 目标: 区分"响应"状态码和"业务"状态码的区别
        // 演示1: 后端用"响应"状态码来区分成功/失败
        // 成功 -> 状态码200 -> 进then
        // 失败 -> 状态码500 -> 进catch
        // document.querySelector('#btnLogin').addEventListener('click', function () {
        //     let username = document.querySelector('#username').value
        //     let password = document.querySelector('#password').value
        //     axios({
        //         url: 'http://ajax-api.itheima.net/api/login',
        //         method: 'POST',
        //         data: {
        //             username: username,
        //             password: password
        //         }
        //     }).then(result => {
        //         // 响应成功进入此函数
        //         console.log(result)
        //         alert('恭喜你，登录成功了')
        //     }).catch(err => {
        //         // 响应失败进入此函数
        //         console.log(err)
        //         alert('账号或密码错误')
        //     })
        // })

        // 演示2: 后端用"业务"状态码来区分成功/失败
        // 成功 -> 状态码200 -> 进then
        // 失败 -> 状态码200 -> 进then
        // 后续用响应体数据对象, code字段值判断
        // 这种方式就叫业务状态码
        document.querySelector('#btnLogin').addEventListener('click', function () {
            let username = document.querySelector('#username').value
            let password = document.querySelector('#password').value
            axios({
                url: 'http://big-event-vue-api-t.itheima.net/api/login',
                method: 'POST',
                data: {
                    username: username,
                    password: password
                }
            }).then(result => {
                console.log(result);
                // 响应成功进入此函数
                if (result.data.code === 0) {
                    alert('恭喜你，登录成功了')
                } else {
                    alert('账号或密码错误')
                }
            })
        })
    </script>
</body>

</html>